package com.zhanggm.zyk.pay.xxl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alipay.api.domain.PaymentVoucherValidPeriodModify;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.zhanggm.zyk.pay.entity.Order;
import com.zhanggm.zyk.pay.entity.PaymentApp;
import com.zhanggm.zyk.pay.service.IOrderService;
import com.zhanggm.zyk.pay.service.IPaymentAppService;
import com.zhanggm.zyk.pay.utils.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class XxlJob {

    @Autowired
    IPaymentAppService iPaymentAppService;

    @Autowired
    IOrderService iOrderService;

    @Autowired
    MailUtil mailUtil;

    /**
     * 定时跑批：每隔10分钟检测是否存在待支付的列表
     */
    @com.xxl.job.core.handler.annotation.XxlJob("demoJobHandler")
    public void xxlJob() {
        log.info("定时任务执行了");
        LambdaQueryWrapper<PaymentApp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentApp::getPaymentStatus, "Pending");
        //where nowtime-time>=1000*60*60*24
//        queryWrapper.

        List<PaymentApp> list = iPaymentAppService.list(queryWrapper);
        list.forEach(paymentApp -> {
//            if (new Date().getTime() - paymentApp.getPayableDate().toInstant(ZoneOffset.of("+8")).toEpochMilli() > 1000 * 60 * 24) {
            if(new Date().getTime()-paymentApp.getPayableDate().getTime()>=1000*60*24){
                mailUtil.sendSimpleMail(paymentApp.getEmail(),"未支付订单通知","请尽快付款，金额："+paymentApp.getThisApplicationAmount());
            }else{

            }
        });
    }

    /**
     * 定时跑批：对账处理，检测付款申请表与收款到账记录表的数据是否匹配，如果存在不匹配的数据则保存到日志当中
     */
    @com.xxl.job.core.handler.annotation.XxlJob("demoJobHandler1")
    public void xxlJob1() {
        //检查订单：金额，支付状态，把有问题订单，写入日志文件中
        List<PaymentApp> paymentAppList = iPaymentAppService.list();
        List<Order> orderList = iOrderService.list();
        //循环申请单，与订单表进行核对
        for (PaymentApp paymentApp : paymentAppList) {
            log.info("申请单对账:data:{}", paymentApp);
            Integer id = paymentApp.getId();
            List<Order> orderList1 = orderList.stream().filter(item -> item.getBusinessId().equals(id + "")).collect(Collectors.toList());
            Order order = CollectionUtil.isEmpty(orderList1) ? null : orderList1.get(0);
            if (order == null) {
                XxlJobHelper.log("申请单对账出错，id:{},data:{}", id, paymentApp);
            } else if (!paymentApp.getThisApplicationAmount().equals(order.getPaymentAmount())) {
                XxlJobHelper.log("申请单对账出错，id:{},data:{}", id, paymentApp);
            } else {
                XxlJobHelper.log("order:{}", id, order);
            }
        }
    }

    public static void main(String[] args) {
        List<PaymentApp> paymentAppList = CollectionUtil.newArrayList();
        paymentAppList.add(new PaymentApp().setPayableDate(new Date()).setId(1));
        List<Order> orderList = CollectionUtil.newArrayList();
        orderList.add(new Order().setBusinessId("1").setId(1));
        List<Order> orderList1 = orderList.stream().filter(item -> item.getBusinessId().equals("1")).collect(Collectors.toList());
        Order order = CollectionUtil.isEmpty(orderList1) ? null : orderList1.get(0);
        if (order == null) {
            System.out.println("空了");
        }else{
            System.out.println("默认值");
        }
        Order order1 = orderList.stream().filter(item -> item.getBusinessId().equals("1")).findFirst().get();
        System.out.println(order1);

    }
}
